package org.jeecg.modules.assets.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;
import com.alibaba.fastjson.util.TypeUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.assets.dto.AssetsListDoubleDTO;
import org.jeecg.modules.assets.dto.AssetsListRandomDTO;
import org.jeecg.modules.assets.dto.OptionPreliminaryAssetsItemDTO;
import org.jeecg.modules.assets.dto.StartAssetsDoubleQuotaionDTO;
import org.jeecg.modules.assets.entity.*;
import org.jeecg.modules.assets.mapper.AssetsDoubleQuotationResultItemMapper;
import org.jeecg.modules.assets.mapper.AssetsDoubleQuotationResultMapper;
import org.jeecg.modules.assets.mapper.AssetsPreliminaryResultsItemMapper;
import org.jeecg.modules.assets.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Description: 异常抽盘管理
 * @Author: jeecg-boot
 * @Date: 2023-12-20
 * @Version: V1.0
 */
@Service
public class AssetsDoubleQuotationResultServiceImpl extends ServiceImpl<AssetsDoubleQuotationResultMapper, AssetsDoubleQuotationResult> implements IAssetsDoubleQuotationResultService {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    SimpleDateFormat sdfdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    @Lazy
    private IAssetsPreliminaryResultsService assetsPreliminaryResultsService;
    @Autowired
    private IAssetsDoubleQuotationResultItemService assetsItemService;
    @Autowired
    private AssetsPreliminaryResultsItemMapper assetsPreliminaryResultsItemMapper;
    @Autowired
    private IDeptExtService iDeptExtService;
    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private WechatUtils wechatUtils;

    @Autowired
    private AssetsDoubleQuotationResultItemMapper assetsDoubleQuotationResultItemMapper;
    @Autowired
    private ISysBaseAPI isysBaseAPI;
    @Autowired
    @Lazy
    private IAssetsItemService itemService;


    @Override
    public Result startAssets(StartAssetsDoubleQuotaionDTO startAssets) {
        //对初盘单号进行校验
        QueryWrapper<AssetsPreliminaryResults> qw = new <AssetsPreliminaryResults>QueryWrapper();
        qw.eq("bill_no", startAssets.getBillNo());
        AssetsPreliminaryResults one = assetsPreliminaryResultsService.getOne(qw);
        if (one == null) {
            throw new RuntimeException("初盘单不存在");
        }
        AssetsDoubleQuotationResult assetsDoubleQuotationResult = new AssetsDoubleQuotationResult();
        assetsDoubleQuotationResult.setDeptName(one.getDeptName());
        assetsDoubleQuotationResult.setDeptId(one.getDeptId());
        assetsDoubleQuotationResult.setPreliminaryBillNo(startAssets.getBillNo());
        assetsDoubleQuotationResult.setDoubleQuotationBadNum(one.getPreliminaryBadNum());
        assetsDoubleQuotationResult.setCreateTime(new Date());
        assetsDoubleQuotationResult.setStartTime(sdfdate.format(new Date()));
        String baseBillNo = "GZFP-" + sdf.format(new Date());
        Object o = redisUtil.get(baseBillNo);
        String formattedNumber;
        if (o == null) {
            redisUtil.set(baseBillNo, 1);
            redisUtil.expire(baseBillNo, 60 * 60 * 24);
            formattedNumber = String.format("%03d", 1);
        } else {
            int i = Integer.parseInt(o.toString()) + 1;
            redisUtil.set(baseBillNo, i);
            redisUtil.expire(baseBillNo, 60 * 60 * 24);
            formattedNumber = String.format("%03d", i);
        }
        assetsDoubleQuotationResult.setBillNo(baseBillNo + "-" + formattedNumber);
        assetsDoubleQuotationResult.setEndTime(startAssets.getEndTime());
        assetsDoubleQuotationResult.setStatus("1");
        //创建复盘单据
        this.save(assetsDoubleQuotationResult);
        //获取初盘异常明细
        QueryWrapper<AssetsPreliminaryResultsItem> preqw = new <AssetsPreliminaryResultsItem>QueryWrapper();
        preqw.eq("apply_id", one.getId()).and(qw1 -> qw1.eq("preliminary_status", 2).or().eq("inventory_status", "0"));
        List<AssetsPreliminaryResultsItem> assetsPreliminaryResultsItems = assetsPreliminaryResultsItemMapper.selectList(preqw);
        if (CollectionUtil.isNotEmpty(assetsPreliminaryResultsItems)) {
            List<AssetsDoubleQuotationResultItem> saveList = new ArrayList<>();
            for (AssetsPreliminaryResultsItem assetsPreliminaryResultsItem : assetsPreliminaryResultsItems) {
                QueryWrapper qwItem = new QueryWrapper();
                qwItem.eq("asset_coding", assetsPreliminaryResultsItem.getAssetCoding());
                qwItem.eq("company", assetsPreliminaryResultsItem.getCompany());
                AssetsItem assetsItem = itemService.getOne(qwItem);

                AssetsDoubleQuotationResultItem assetsDoubleQuotationResultItem = new AssetsDoubleQuotationResultItem();
                assetsDoubleQuotationResultItem.setCompany(assetsItem.getCompany());
                assetsDoubleQuotationResultItem.setCompany(assetsItem.getCompanyName());
                assetsDoubleQuotationResultItem.setAssetsNum(assetsPreliminaryResultsItem.getAssetsNum());
                assetsDoubleQuotationResultItem.setAssetName(assetsPreliminaryResultsItem.getAssetName());
                assetsDoubleQuotationResultItem.setSpec(assetsPreliminaryResultsItem.getSpec());
                assetsDoubleQuotationResultItem.setUserId(assetsPreliminaryResultsItem.getUserId());
                assetsDoubleQuotationResultItem.setUserer(assetsItem.getUserer());
                assetsDoubleQuotationResultItem.setStatus(assetsPreliminaryResultsItem.getStatus());
                assetsDoubleQuotationResultItem.setUseTime(assetsPreliminaryResultsItem.getUseTime());
                assetsDoubleQuotationResultItem.setOwner(assetsItem.getOwner());
                assetsDoubleQuotationResultItem.setOwnerId(assetsPreliminaryResultsItem.getOwnerId());
                assetsDoubleQuotationResultItem.setAssetCoding(assetsPreliminaryResultsItem.getAssetCoding());
                assetsDoubleQuotationResultItem.setInventoryStatus("0");
                assetsDoubleQuotationResultItem.setApplyId(assetsDoubleQuotationResult.getId());
                assetsDoubleQuotationResultItem.setDeptId(assetsItem.getDept());
                assetsDoubleQuotationResultItem.setDeptName(assetsItem.getSimpleDeptName());
                assetsDoubleQuotationResultItem.setInventoryType(assetsPreliminaryResultsItem.getInventoryType());
                assetsDoubleQuotationResultItem.setCategory(assetsItem.getCategory());
                assetsDoubleQuotationResultItem.setAssetsLocation(assetsItem.getAssetsLocation());
                //添加公司属性
                assetsDoubleQuotationResultItem.setCompany(assetsItem.getCompany());
                assetsDoubleQuotationResultItem.setCompanyName(assetsItem.getCompanyName());

                if (Objects.equals(assetsPreliminaryResultsItem.getInventoryType(), 2)) {
                    assetsDoubleQuotationResultItem.setInventoryStatus("1");
                    assetsDoubleQuotationResultItem.setPreliminaryStatus(1);
                }
                saveList.add(assetsDoubleQuotationResultItem);
            }
            assetsItemService.saveBatch(saveList);
            Map<String, Integer> map = new HashMap<>();
            //根据初盘明细。组装企微消息发送
            ThreadUtil.execAsync(new Runnable() {
                @Override
                public void run() {
                    for (AssetsDoubleQuotationResultItem assetsPreliminaryResultsItem : saveList) {
                        Integer countUser = map.get(assetsPreliminaryResultsItem.getUserer());
                        if (countUser != null) {
                            map.put(assetsPreliminaryResultsItem.getUserer(), countUser + 1);
                        } else {
                            map.put(assetsPreliminaryResultsItem.getUserer(), 1);
                        }
                    }
                    //根据map发送消息
                    for (Map.Entry<String, Integer> stringIntegerEntry : map.entrySet()) {
                        LoginUser userByRealName = isysBaseAPI.getUserByRealName(stringIntegerEntry.getKey());
                        wechatUtils.sendDoubleQuotaionWechatMessage(userByRealName.getId(), assetsDoubleQuotationResult.getBillNo(), TypeUtils.castToString(stringIntegerEntry.getValue()), assetsDoubleQuotationResult.getEndTime());
                    }
                }
            });
        }

        return Result.OK("发起成功", assetsDoubleQuotationResult.getBillNo());

    }

    @Override
    public AssetsListDoubleDTO getUsetAssetsInfo(AssetsDoubleQuotationResult doubleQuotationResult, Page page, Integer queryType, String owner, String inventoryStatus, Integer preliminaryStatus,String assetName,String company) {
        //获取当前用户
        Long badNum = 0L;
        AssetsListDoubleDTO assetsListDTO = new AssetsListDoubleDTO();
        assetsListDTO.setEndTime(doubleQuotationResult.getEndTime());
        assetsListDTO.setDeptName(assetsListDTO.getDeptName());
        assetsListDTO.setUserName(assetsListDTO.getUserName());
        assetsListDTO.setStatus(doubleQuotationResult.getStatus());
        if (Objects.equals(1, queryType)) {
            //获取当前登录人
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            Page<AssetsDoubleQuotationResultItem> assetsItem = assetsDoubleQuotationResultItemMapper.getAssetsItem(page, doubleQuotationResult.getId(), user.getRealname(), inventoryStatus, preliminaryStatus,assetName,company);
            assetsListDTO.setItemPages(assetsItem);
            //获取初盘异常数量
            QueryWrapper<AssetsDoubleQuotationResultItem> badNumQw = new <AssetsDoubleQuotationResultItem>QueryWrapper();
            badNumQw.eq("apply_id", doubleQuotationResult.getId());
            badNumQw.and(item -> {
                item.eq("owner", user.getRealname());
                item.or().eq("userer", user.getRealname());

            });
            badNum = assetsDoubleQuotationResultItemMapper.selectCount(badNumQw);
            assetsListDTO.setBadNum(TypeUtils.castToInt(badNum));
            QueryWrapper<AssetsDoubleQuotationResultItem> haveNum = new <AssetsDoubleQuotationResultItem>QueryWrapper();
            haveNum.eq("apply_id", doubleQuotationResult.getId());
            haveNum.and(qw2 -> qw2.eq("userer", user.getRealname()).or().eq("owner", user.getRealname()));
            haveNum.eq("inventory_status", "1");
            Long aLong = assetsDoubleQuotationResultItemMapper.selectCount(haveNum);
            assetsListDTO.setHaveNum(TypeUtils.castToInt(aLong));
            assetsListDTO.setDoubleBadNum(assetsListDTO.getBadNum() - assetsListDTO.getHaveNum());
            return assetsListDTO;

        } else {
            QueryWrapper<AssetsDoubleQuotationResultItem> applyId = new QueryWrapper<AssetsDoubleQuotationResultItem>().eq("apply_id", doubleQuotationResult.getId());
            badNum = assetsDoubleQuotationResultItemMapper.selectCount(applyId);
            if (StringUtils.isNotBlank(owner)) {
                applyId.like("owner", owner);
            }
            if (StringUtils.isNotBlank(inventoryStatus)) {
                applyId.eq("inventory_status", inventoryStatus);
            }
            if (preliminaryStatus != null) {
                applyId.eq("preliminary_status", preliminaryStatus);
            }
            Page assetsItem = assetsDoubleQuotationResultItemMapper.selectPage(page, applyId);
            assetsListDTO.setItemPages(assetsItem);
            assetsListDTO.setBadNum(TypeUtils.castToInt(badNum));
            QueryWrapper haveNum = new QueryWrapper();
            haveNum.eq("apply_id", doubleQuotationResult.getId());
            haveNum.eq("inventory_status", "1");
            Long aLong = assetsDoubleQuotationResultItemMapper.selectCount(haveNum);
            assetsListDTO.setHaveNum(TypeUtils.castToInt(aLong));
            QueryWrapper doublebadNum = new QueryWrapper();
            doublebadNum.eq("apply_id", doubleQuotationResult.getId());
            doublebadNum.eq("preliminary_status", 2);
            Long badCount = assetsDoubleQuotationResultItemMapper.selectCount(doublebadNum);
            assetsListDTO.setDoubleBadNum(TypeUtils.castToInt(badCount));
            return assetsListDTO;
        }


    }

    @Override
    public void optionAssetsItem(OptionPreliminaryAssetsItemDTO optionPreliminaryAssetsItem) {
        String msg = "";
        //获取当前登录人
        //单据单据编号，获取复盘单号
        AssetsDoubleQuotationResult assetsDoubleQuotationResult = this.getOne(new QueryWrapper<AssetsDoubleQuotationResult>().eq("bill_no", optionPreliminaryAssetsItem.getBillNo()));
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        QueryWrapper qw = new QueryWrapper();
        qw.eq("apply_id", assetsDoubleQuotationResult.getId());
        qw.eq("userer", user.getRealname());
        qw.eq("asset_coding", optionPreliminaryAssetsItem.getAssetCoding());
        AssetsDoubleQuotationResultItem assetsDoubleQuotationResultItem = assetsDoubleQuotationResultItemMapper.selectOne(qw);
        if (assetsDoubleQuotationResultItem == null) {
            msg = "资产部门不匹配";
            qw = new QueryWrapper();
            qw.eq("unique_id", assetsDoubleQuotationResult.getUniqueId());
            qw.eq("userer", user.getRealname());
            qw.eq("asset_coding", optionPreliminaryAssetsItem.getAssetCoding());
            assetsDoubleQuotationResultItem = assetsDoubleQuotationResultItemMapper.selectOne(qw);
            if (assetsDoubleQuotationResultItem == null) {
                throw new RuntimeException("资产部门不匹配，并且使用人也不匹配");
            }

        }
        //通过字典配置公司校验是否需要盘点资产图片
        List<DictModel> lstDic= isysBaseAPI.queryDictItemsByCode("company_photo");
        if(lstDic!=null&&lstDic.stream().count()>0){
            String companyCode=assetsDoubleQuotationResultItem.getCompany();
            List<DictModel> streamDicModel=lstDic.stream().filter(x->x.getValue().equals(companyCode)).collect(Collectors.toList());
            if(streamDicModel!=null&&streamDicModel.stream().count()>0){
                if(org.apache.commons.lang3.StringUtils.isEmpty(optionPreliminaryAssetsItem.getAssetImageUrl())){
                    throw new RuntimeException("公司【"+companyCode+"】盘点资产需要上传资产图片");
                }
            }
        }
        assetsDoubleQuotationResultItem.setAssetImageUrl(optionPreliminaryAssetsItem.getAssetImageUrl());
        //修改复盘明细
        assetsDoubleQuotationResultItem.setPreliminaryStatus(1);
        if (!StringUtils.isNotBlank(msg)) {
            assetsDoubleQuotationResultItem.setMsgDesc(msg);
        }
        assetsDoubleQuotationResultItem.setInventoryStatus("1");
        assetsDoubleQuotationResultItemMapper.updateById(assetsDoubleQuotationResultItem);

    }

    /**
     * 复盘结束任务
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void endDoubleEndAssets() {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("status", 1);
        String format = sdfdate.format(new Date());
        qw.le("end_time", format);
        List<AssetsDoubleQuotationResult> list = this.list(qw);
        if (CollectionUtil.isNotEmpty(list)) {
            for (AssetsDoubleQuotationResult assetsDoubleQuotationResult : list) {
                QueryWrapper prelimaryQw = new QueryWrapper();
                prelimaryQw.eq("bill_no", assetsDoubleQuotationResult.getPreliminaryBillNo());
                AssetsPreliminaryResults assetsPreliminaryResults = assetsPreliminaryResultsService.getOne(prelimaryQw);
                //修改状态
                assetsDoubleQuotationResult.setStatus("2");
                //直接更新未盘明细未异常
                UpdateWrapper updateWrapper = new UpdateWrapper();
                updateWrapper.eq("apply_id", assetsDoubleQuotationResult.getId());
                updateWrapper.eq("inventory_status", "0");
                updateWrapper.set("preliminary_status", 2);

                assetsDoubleQuotationResultItemMapper.update(null, updateWrapper);

                //根据复盘单号，获取复盘结果
                QueryWrapper qwItem = new QueryWrapper();
                qwItem.eq("apply_id", assetsDoubleQuotationResult.getId());
                List<AssetsDoubleQuotationResultItem> assetsDoubleQuotationResultItems = assetsDoubleQuotationResultItemMapper.selectList(qwItem);
                if (CollectionUtil.isNotEmpty(assetsDoubleQuotationResultItems)) {
                    long badCount = assetsDoubleQuotationResultItems.stream().filter(item -> Objects.equals(item.getPreliminaryStatus(), 2)).count();
                    if (badCount > 0) {
                        assetsDoubleQuotationResult.setDoubleQuotationBadNum(TypeUtils.castToInt(badCount));
                        assetsPreliminaryResults.setDoubleQuotationBadNum(TypeUtils.castToInt(badCount));
                        assetsDoubleQuotationResult.setStatus("3");
                    } else {
                        assetsDoubleQuotationResult.setDoubleQuotationBadNum(0);
                        assetsPreliminaryResults.setDoubleQuotationBadNum(0);
                        assetsDoubleQuotationResult.setStatus("2");
                    }
                    assetsDoubleQuotationResult.setDoubleQuotationNum(assetsDoubleQuotationResultItems.size());
                    assetsPreliminaryResults.setDoubleQuotationNum(assetsDoubleQuotationResultItems.size());
                }
                this.updateById(assetsDoubleQuotationResult);
                assetsPreliminaryResultsService.updateById(assetsPreliminaryResults);
                //根据部门id获取部门领导
                LambdaQueryWrapper<DeptExt> lambda = new QueryWrapper<DeptExt>().lambda();
                lambda.eq(DeptExt::getDeptId, assetsPreliminaryResults.getDeptId());
                DeptExt dept = iDeptExtService.getOne(lambda);
                if(dept!=null){
                    wechatUtils.sendDoubleQuotaionWechatEndMessage(dept.getDeptUserId(), assetsDoubleQuotationResult);
                    wechatUtils.sendDoubleQuotaionWechatEndMessage(dept.getAssetsUserId(), assetsDoubleQuotationResult);
                }

            }
        }

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void startAssetsAdmin(StartAssetsDoubleQuotaionDTO startAssets) {
        //对初盘单号进行校验
        QueryWrapper<AssetsPreliminaryResults> qw = new <AssetsPreliminaryResults>QueryWrapper();
        qw.eq("bill_no", startAssets.getBillNo());
        AssetsPreliminaryResults one = assetsPreliminaryResultsService.getOne(qw);
        if (one == null) {
            throw new RuntimeException("初盘单不存在");
        }

        AssetsDoubleQuotationResult assetsDoubleQuotationResult = new AssetsDoubleQuotationResult();
        assetsDoubleQuotationResult.setDeptName(one.getDeptName());
        assetsDoubleQuotationResult.setDeptId(one.getDeptId());
        assetsDoubleQuotationResult.setPreliminaryBillNo(startAssets.getBillNo());
        assetsDoubleQuotationResult.setDoubleQuotationBadNum(one.getPreliminaryBadNum());
        assetsDoubleQuotationResult.setCreateTime(new Date());
        assetsDoubleQuotationResult.setStartTime(sdfdate.format(new Date()));
        String baseBillNo = "GZFP-" + sdf.format(new Date());
        Object o = redisUtil.get(baseBillNo);
        String formattedNumber;
        if (o == null) {
            redisUtil.set(baseBillNo, 1);
            redisUtil.expire(baseBillNo, 60 * 60 * 24);
            formattedNumber = String.format("%03d", 1);
        } else {
            int i = Integer.parseInt(o.toString()) + 1;
            redisUtil.set(baseBillNo, i);
            redisUtil.expire(baseBillNo, 60 * 60 * 24);
            formattedNumber = String.format("%03d", i);
        }
        assetsDoubleQuotationResult.setBillNo(baseBillNo + "-" + formattedNumber);
        assetsDoubleQuotationResult.setEndTime(startAssets.getEndTime());
        assetsDoubleQuotationResult.setStatus("1");
        //创建复盘单据
        this.save(assetsDoubleQuotationResult);

        //获取初盘异常明细
        QueryWrapper<AssetsPreliminaryResultsItem> preqw = new <AssetsPreliminaryResultsItem>QueryWrapper();
        preqw.eq("apply_id", one.getId()).and(qw1 -> qw1.eq("preliminary_status", 2).or().eq("inventory_status", "0"));
        List<AssetsPreliminaryResultsItem> assetsPreliminaryResultsItems = assetsPreliminaryResultsItemMapper.selectList(preqw);
        if (CollectionUtil.isNotEmpty(assetsPreliminaryResultsItems)) {
            List<AssetsDoubleQuotationResultItem> saveList = new ArrayList<>();
            for (AssetsPreliminaryResultsItem assetsPreliminaryResultsItem : assetsPreliminaryResultsItems) {
                QueryWrapper qwItem = new QueryWrapper();
                qwItem.eq("asset_coding", assetsPreliminaryResultsItem.getAssetCoding());
                qwItem.eq("company", assetsPreliminaryResultsItem.getCompany());
                AssetsItem assetsItem = itemService.getOne(qwItem);
                AssetsDoubleQuotationResultItem assetsDoubleQuotationResultItem = new AssetsDoubleQuotationResultItem();
                assetsDoubleQuotationResultItem.setAssetsNum(assetsPreliminaryResultsItem.getAssetsNum());
                assetsDoubleQuotationResultItem.setAssetName(assetsPreliminaryResultsItem.getAssetName());
                assetsDoubleQuotationResultItem.setSpec(assetsPreliminaryResultsItem.getSpec());
                assetsDoubleQuotationResultItem.setCompany(assetsItem.getCompany());
                assetsDoubleQuotationResultItem.setCompany(assetsItem.getCompanyName());
                assetsDoubleQuotationResultItem.setUserId(assetsPreliminaryResultsItem.getUserId());
                assetsDoubleQuotationResultItem.setUserer(assetsItem.getUserer());
                assetsDoubleQuotationResultItem.setStatus(assetsPreliminaryResultsItem.getStatus());
                assetsDoubleQuotationResultItem.setUseTime(assetsPreliminaryResultsItem.getUseTime());
                assetsDoubleQuotationResultItem.setOwner(assetsItem.getOwner());
                assetsDoubleQuotationResultItem.setOwnerId(assetsPreliminaryResultsItem.getOwnerId());
                assetsDoubleQuotationResultItem.setAssetCoding(assetsPreliminaryResultsItem.getAssetCoding());
                assetsDoubleQuotationResultItem.setInventoryStatus("0");
                assetsDoubleQuotationResultItem.setApplyId(assetsDoubleQuotationResult.getId());
                assetsDoubleQuotationResultItem.setDeptId(assetsDoubleQuotationResult.getDeptId());
                assetsDoubleQuotationResultItem.setDeptName(assetsDoubleQuotationResult.getDeptName());
                assetsDoubleQuotationResultItem.setInventoryType(assetsPreliminaryResultsItem.getInventoryType());
                assetsDoubleQuotationResultItem.setCategory(assetsItem.getCategory());
                assetsDoubleQuotationResultItem.setAssetsLocation(assetsItem.getAssetsLocation());
                //添加公司属性
                assetsDoubleQuotationResultItem.setCompany(assetsPreliminaryResultsItem.getCompany());
                assetsDoubleQuotationResultItem.setCompanyName(assetsPreliminaryResultsItem.getCompanyName());
                assetsDoubleQuotationResultItem.setUniqueId(assetsPreliminaryResultsItem.getUniqueId());
                if (Objects.equals(assetsPreliminaryResultsItem.getInventoryType(), 2)) {
                    assetsDoubleQuotationResultItem.setInventoryStatus("1");
                    assetsDoubleQuotationResultItem.setPreliminaryStatus(1);
                }
                saveList.add(assetsDoubleQuotationResultItem);
            }
            assetsItemService.saveBatch(saveList);
            Map<String, Integer> map = new HashMap<>();
            //根据初盘明细。组装企微消息发送
            ThreadUtil.execAsync(new Runnable() {
                @Override
                public void run() {
                    for (AssetsPreliminaryResultsItem assetsPreliminaryResultsItem : assetsPreliminaryResultsItems) {
                        Integer countUser = map.get(assetsPreliminaryResultsItem.getUserer());
                        if (countUser != null) {
                            map.put(assetsPreliminaryResultsItem.getUserer(), countUser + 1);
                        } else {
                            map.put(assetsPreliminaryResultsItem.getUserer(), 1);
                        }
                    }
                    //根据map发送消息
                    for (Map.Entry<String, Integer> stringIntegerEntry : map.entrySet()) {
                        LoginUser userByRealName = isysBaseAPI.getUserByRealName(stringIntegerEntry.getKey());
                        wechatUtils.sendDoubleQuotaionWechatMessage(userByRealName.getId(), assetsDoubleQuotationResult.getBillNo(), TypeUtils.castToString(stringIntegerEntry.getValue()), assetsDoubleQuotationResult.getEndTime());
                    }
                }
            });
        }


    }


}
